package com.zhiwang.xiaoqu.ui.fragment


import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.zhiwang.xiaoqu.R
import com.zhiwang.xiaoqu.di.annotation.Injectable
import com.zhiwang.xiaoqu.mvp.model.Store
import com.zhiwang.xiaoqu.mvp.model.response.MyUserInfo
import com.zhiwang.xiaoqu.mvp.presenter.UserPresenter
import com.zhiwang.xiaoqu.ui.AccountSettingActivity
import com.zhiwang.xiaoqu.ui.BaseFragment
import com.zhiwang.xiaoqu.ui.MoneyDetailListActivity
import com.zhiwang.xiaoqu.ui.WebViewActivity
import com.zhiwang.xiaoqu.ui.about.AboutActivity
import com.zhiwang.xiaoqu.ui.about.ServiceActivity
import com.zhiwang.xiaoqu.ui.coupon.CouponListActivity
import com.zhiwang.xiaoqu.ui.message.MessageListActivity
import com.zhiwang.xiaoqu.ui.my.ChangePhoneActivity
import com.zhiwang.xiaoqu.ui.my.FriendListActivity
import com.zhiwang.xiaoqu.ui.withdraw.WithDrawActivity
import com.zhiwang.xiaoqu.ui.withdraw.WithdrawListActivity
import com.zhiwang.xiaoqu.widget.loopviewpager.LoopViewPager
import com.zhiwang.xiaoqu.widget.loopviewpager.SimpleNetWorkLoopingPagerAdapter
import com.zhiwang.xiaoqu.widget.transformer.ViewPagerGalleryTransform
import kotlinx.android.synthetic.main.fragment_my.*
import quickStartActivity
import javax.inject.Inject

/**
 * A simple [Fragment] subclass.
 */
class MyFragment : BaseFragment(), View.OnClickListener, Injectable {

    @Inject
    lateinit var userPresenter: UserPresenter

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_my, container, false)
    }


    override fun onStart() {
        super.onStart()
        my_banner.adapter = SimpleNetWorkLoopingPagerAdapter(
            requireContext(),
            arrayListOf(
                "https://github.com/devWilly/InfiniteViewPagerEx/blob/master/app/src/main/res/drawable-xhdpi/pic1.png?raw=true",
                "https://github.com/devWilly/InfiniteViewPagerEx/blob/master/app/src/main/res/drawable-xhdpi/pic3.png?raw=true",
                "https://github.com/devWilly/InfiniteViewPagerEx/blob/master/app/src/main/res/drawable-xhdpi/pic2.png?raw=true"
            )
            , true
        )
        my_banner.setPageTransformer(false, ViewPagerGalleryTransform())
        my_banner.setIndicatorPageChangeListener(object : LoopViewPager.IndicatorPageChangeListener {
            override fun onIndicatorProgress(selectingPosition: Int, progress: Float) {

            }

            override fun onIndicatorPageChange(newIndicatorPosition: Int) {

                my_banner_indicator.position = newIndicatorPosition
            }

        })

        tv_withdraw_btn.setOnClickListener(this)
        tips.setOnClickListener(this)
        account_detail.setOnClickListener(this)
        account_setting.setOnClickListener(this)
        about.setOnClickListener(this)
        my_card.setOnClickListener(this)
        my_message.setOnClickListener(this)
        withdraw_record.setOnClickListener(this)
        my_friend.setOnClickListener(this)
        service_center.setOnClickListener(this)
        change_phone.setOnClickListener(this)
        userPresenter.getUserInfo(::onLoadUserInfoSuccess, ::onError)
    }

    @SuppressLint("SetTextI18n")
    private fun onLoadUserInfoSuccess(userInfo: MyUserInfo) {
        tv_balance.text = userInfo.income ?: "0"
        tv_today.text = "今日收入 +${userInfo.todayIncome ?: 0}元"
        tv_red_packet_income.text = "已获得红包:${userInfo.redPacketIncome ?: 0}元"
    }


    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.tv_withdraw_btn -> quickStartActivity(WithDrawActivity::class.java)
            R.id.tips -> WebViewActivity.load(requireContext(), Store.URL_NEWBIE_TIPS)
            R.id.account_detail -> quickStartActivity(MoneyDetailListActivity::class.java)
            R.id.account_setting -> quickStartActivity(AccountSettingActivity::class.java)
            R.id.about -> quickStartActivity(AboutActivity::class.java)
            R.id.my_card -> quickStartActivity(CouponListActivity::class.java)
            R.id.my_message -> quickStartActivity(MessageListActivity::class.java)
            R.id.withdraw_record -> quickStartActivity(WithdrawListActivity::class.java)
            R.id.my_friend -> quickStartActivity(FriendListActivity::class.java)
            R.id.service_center -> quickStartActivity(ServiceActivity::class.java)
            R.id.change_phone -> quickStartActivity(ChangePhoneActivity::class.java)
        }
    }


}
